﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace IntelligentCMS
{
    public partial class managefields : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //@todo: update to make use of Operation strings
            if (Request.QueryString["ID"] != null)
            {
                if (Request.QueryString["ID"].Trim() != string.Empty)
                {
                    string deleteid = Request.QueryString["ID"];

                    CMSDbDataLayer delete = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                    delete.AddQuery("DELETE FROM Field WHERE ID = " + deleteid);
                    delete.SelectQuery = false;
                    delete.Execute();


                }

            }



            CMSDbDataLayer field = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            field.AddQuery("SELECT [ID], [Name], [Description], [ID] AS [Edit], [ID] AS [Delete] FROM Field");
            field.SelectQuery = true;
            field.Execute();

            List<CMSColumnType> newCMSColumnTypes = new List<CMSColumnType>();
            CMSColumnType CMSColumnType_ID = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Text, string.Empty, string.Empty);
            CMSColumnType CMSColumnType_Name = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Text, string.Empty, string.Empty);
            CMSColumnType CMSColumnType_Description = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Text, string.Empty, string.Empty);
            CMSColumnType CMSColumnType_Edit = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Hyperlink, "field.aspx?ID={0}", "edit");
            CMSColumnType CMSColumnType_Delete = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Hyperlink, "managefields.aspx?ID={0}", "delete");

            newCMSColumnTypes.Add(CMSColumnType_ID);
            newCMSColumnTypes.Add(CMSColumnType_Name);
            newCMSColumnTypes.Add(CMSColumnType_Description);
            newCMSColumnTypes.Add(CMSColumnType_Edit);
            newCMSColumnTypes.Add(CMSColumnType_Delete);

            for (int i = 0; i < field.Data.Rows.Count; i++)
            {
                field.Data.Rows[i]["ID"] = field.Data.Rows[i]["ID"].ToString();
                field.Data.Rows[i]["Name"] = common.FromBase64String(field.Data.Rows[i]["Name"].ToString());
                field.Data.Rows[i]["Description"] = common.FromBase64String(field.Data.Rows[i]["Description"].ToString());
                field.Data.Rows[i]["Edit"] = field.Data.Rows[i]["ID"].ToString();
                field.Data.Rows[i]["Delete"] = field.Data.Rows[i]["ID"].ToString();

            }

            theCMSDataGrid.Data = field.Data;
            theCMSDataGrid.CMSColumnTypes = newCMSColumnTypes;
            theCMSDataGrid.Execute();

        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            Response.Redirect("field.aspx");
        }

        protected void Button_Done_Click(object sender, EventArgs e)
        {
            Response.Redirect("contenttype.aspx");
        }
    }
}
